//
//  NSString+Attributed.m
//  jzyParents
//
//  Created by 任辰东 on 2022/4/29.
//

#import "NSString+Attributed.h"

@implementation NSString (Attributed)

// 设置字符串中指定字体颜色 大小
- (NSAttributedString *)setAttributedStringWith:(NSString *)string WithIndexes:(NSString *)indexes WithStringColor:(UIColor *)stringColor WithIndexesColor:(UIColor *)indexesColor WithFont:(UIFont *)font
{
    // 创建对象.
    NSMutableAttributedString *mAttStri = [[NSMutableAttributedString alloc] initWithString:string];
    //
    NSRange range = [string rangeOfString:indexes];
    [mAttStri addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(0, string.length)];
    [mAttStri addAttribute:NSFontAttributeName value:font range:range];
    [mAttStri addAttribute:NSForegroundColorAttributeName value:indexesColor range:range];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];        //设置行间距
//    [paragraphStyle setLineBreakMode:self.lineBreakMode];
    [mAttStri addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
    return mAttStri;
}

// 设置字符串中指定字体颜色 大小
+ (NSAttributedString *)setAttributedStringWith:(NSString *)string WithIndexes:(NSString *)indexes WithStringColor:(UIColor *)stringColor WithIndexesColor:(UIColor *)indexesColor WithFont:(UIFont *)font
{
    // 创建对象.
    NSMutableAttributedString *mAttStri = [[NSMutableAttributedString alloc] initWithString:string];
    //
    NSRange range = [string rangeOfString:indexes];
    [mAttStri addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(0, string.length)];
    [mAttStri addAttribute:NSFontAttributeName value:font range:range];
    [mAttStri addAttribute:NSForegroundColorAttributeName value:indexesColor range:range];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];        //设置行间距
//    [paragraphStyle setLineBreakMode:self.lineBreakMode];
    [mAttStri addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
    return mAttStri;
}

+ (NSAttributedString *)setAttributedStringWith:(NSString *)string WithIndexes1:(NSString *)indexes1 WithIndexes2:(NSString *)indexes2 WithStringColor:(UIColor *)stringColor WithIndexes1Color:(UIColor *)indexes1Color WithIndexes2Color:(UIColor *)indexes2Color
{
    // 创建对象.
    NSMutableAttributedString *mAttStri = [[NSMutableAttributedString alloc] initWithString:string];

    [mAttStri addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(0, string.length)];
    
    NSRange range1 = [string rangeOfString:indexes1];
    [mAttStri addAttribute:NSForegroundColorAttributeName value:indexes1Color range:range1];
    
    NSRange range2 = [string rangeOfString:indexes2];
    [mAttStri addAttribute:NSForegroundColorAttributeName value:indexes2Color range:range2];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];        //设置行间距
//    [paragraphStyle setLineBreakMode:self.lineBreakMode];
    [mAttStri addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
    return mAttStri;
}

@end
